<template>
  <div class="login-container">
    <el-form
      ref="loginForm"
      :model="loginForm"
      :rules="loginRules"
      class="login-form"
      autocomplete="on"
      size="small"
      label-position="left"
    >
      <div class="logo-container">
        <div class="logo"></div>
      </div>
      <div class="title-container">
        <div class="title">
          达坦官网管理平台
        </div>
      </div>
      <el-form-item prop="username">
        <el-input
          ref="username"
          v-model="loginForm.username"
          placeholder="用户名"
          name="username"
          type="text"
          tabindex="1"
        />
      </el-form-item>

      <el-tooltip
        v-model="capsTooltip"
        content="大写锁定已打开"
        placement="right"
        manual
      >
        <el-form-item prop="password">
          <el-input
            :key="passwordType"
            ref="password"
            v-model="loginForm.password"
            :type="passwordType"
            placeholder="密码"
            name="password"
            tabindex="2"
            autocomplete="on"
            @keyup.native="checkCapslock"
            @blur="capsTooltip = false"
            @keyup.enter.native="handleLogin"
          />
          <span class="show-pwd" @click="showPwd">
            <svg-icon
              :name="passwordType === 'password' ? 'eye-off' : 'eye-on'"
            />
          </span>
        </el-form-item>
      </el-tooltip>
      <div style="width:100%;display:flex;justify-content:center">
        <el-button
          :loading="loading"
          type="primary"
          class="login-btn"
          style=""
          @click.native.prevent="handleLogin"
        >
          登 录
        </el-button>
      </div>
    </el-form>
  </div>
</template>

<script>
import { Login } from "../../../api/admin";
export default {
  name: "Login",
  data() {
    return {
      loginForm: {
        username: "",
        password: "",
      },
      loginRules: {
        username: [{ required: true, message: "请输入用户名" }],
        password: [{ validator: this.validatePassword }],
      },
      passwordType: "password",
      loading: false,
      capsTooltip: false,
    };
  },
  mounted() {
    if (this.loginForm.username === "") {
      this.$refs.username.focus();
    } else if (this.loginForm.password === "") {
      this.$refs.password.focus();
    }
  },
  methods: {
    validatePassword(rule, value, callback) {
      if (value.length < 1) {
        callback(new Error("请输入密码"));
      } else {
        callback();
      }
      callback();
    },
    checkCapslock(e) {
      const { key } = e;
      this.capsTooltip =
        key !== null &&
        key !== undefined &&
        key.length === 1 &&
        key >= "A" &&
        key <= "Z";
    },
    showPwd() {
      if (this.passwordType === "password") {
        this.passwordType = "";
      } else {
        this.passwordType = "password";
      }
      this.$nextTick(() => {
        this.$refs.password.focus();
      });
    },
    handleLogin() {
      this.$refs.loginForm.validate(async (valid) => {
        if (valid) {
          this.loading = true;
          try {
            let form = new FormData();
            form.append("username", this.loginForm.username);
            form.append("password", this.loginForm.password);
            Login(form);
            this.$router
              .push({
                path: "/",
              })
              .catch((err) => {
                console.warn(err);
              });
            this.loading = false;
          } catch (err) {
            console.log(err);
            this.loading = false;
          }

          // Just to simulate the time of the request
        } else {
          return false;
        }
      });
    },
  },
};
</script>

<style lang="scss">
@supports (-webkit-mask: none) and (not (cater-color: #fff)) {
  .login-container .el-input {
  }
}

.login-container {
  .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;

    input {
      height: 47px;
      background: transparent;
      border: 0px;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: #eee;
      -webkit-appearance: none;

      &:-webkit-autofill {
        background-color: transparent;
        -webkit-transition-delay: 999999s;
        -webkit-transition: color 999999s ease-out,
          background-color 999999s ease-out;
      }
    }
  }
  .el-form-item__error {
    padding-left: 40px;
  }
  .el-form-item {
    box-shadow: 0px 0px 29px 0px #008bff inset;
    border-radius: 22px;
  }
}
</style>

<style lang="scss" scoped>
.login-container {
  height: 100%;
  width: 100%;
  overflow: hidden;
  background: url("./bg.png");
  background-size: cover;
  background-position: bottom left;
  .login-form {
    box-sizing: border-box;
    position: relative;
    width: 460px;
    max-width: 100%;
    padding: 35px 75px 35px 75px;
    margin: 100px auto;
    overflow: hidden;
  }

  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;

    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }

  .svg-container {
    padding: 4px 5px 6px 15px;
    color: #889aa4;
    vertical-align: middle;
    width: 30px;
    // display: inline-block;
    display: none;
  }
  .title-container {
    position: relative;
    width: 100%;
    margin-top: 46px;
    margin-bottom: 76px;
    .title {
      width: 100%;
      text-align: center;
      height: 24px;
      font-size: 24px;
      font-family: SourceHanSansCN-Regular, SourceHanSansCN;
      font-weight: 400;
      color: #ffffff;
      line-height: 36px;
      letter-spacing: 1px;
    }
  }
  .logo-container {
    position: relative;

    .logo {
      width: 100%;
      height: 76px;
      background: url("./logo.png") no-repeat center center;
      background-size: contain;
    }
  }
  .login-btn {
    width: 140px;
    height: 44px;
    background-color: #0057a0;
    border-radius: 22px;
    margin: 20px;
    border: none;
    &:hover {
      background-color: #176aad;
    }
    &:active {
      border: 1px solid #dcdfe6;
    }
  }
  .show-pwd {
    position: absolute;
    right: 10px;
    top: 9px;
    font-size: 16px;
    color: #889aa4;
    cursor: pointer;
    user-select: none;
  }

  .thirdparty-button {
    position: absolute;
    right: 0;
    bottom: 6px;
  }

  @media only screen and (max-width: 470px) {
    .thirdparty-button {
      display: none;
    }
  }
}
</style>
